﻿using Domain;
using Domain.Entity;
using Domain.Repository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
	public class ERRepository:IERRepository
	{
		private readonly MyDbContext myDbContext;

		public ERRepository(MyDbContext myDbContext)
		{
			this.myDbContext = myDbContext;
		}

		public async Task<bool> AddERAsync(ER er)
		{
			er.CheckStatus = 1;
			myDbContext.ER.Add(er);
			int result = await myDbContext.SaveChangesAsync();
			return result > 0;
		}

		public ERFenYE DateERSSAsync(string HumanMajorKindId, string HumanMajorId, string gzc, DateTime startDate, DateTime endDate, int page, int pageSize,int zt,int zt1)
		{
			var query = myDbContext.ER.AsQueryable();

			if (!string.IsNullOrEmpty(HumanMajorKindId))
			{
				query = query.Where(s => s.HumanMajorKindId == HumanMajorKindId);
			}
			if (!string.IsNullOrEmpty(HumanMajorId))
			{
				query = query.Where(s => s.HumanMajorId == HumanMajorId);
			}
			if (!string.IsNullOrEmpty(gzc))
			{
				query = query.Where(s => s.HumanName.Contains(gzc));
			}

			if (startDate != default && endDate != default)
			{
				query = query.Where(s => s.RegistTime >= startDate && s.RegistTime <= endDate);
			}
			if (!string.IsNullOrEmpty(zt.ToString()))
			{
				query = query.Where(s => s.CheckStatus == zt);
			}
			if (!string.IsNullOrEmpty(zt1.ToString()))
			{
				query = query.Where(s => s.InterviewStatus == zt1);
			}
			int totalRecords = query.Count();
			var ER = query.Skip((page - 1) * pageSize).Take(pageSize).ToList();
			return new ERFenYE
			{
				totalRecords = totalRecords,
				er = ER
			};
		}

		public ERFenYE DateERSAsync(int page, int pageSize, int zt)
		{
			var query = myDbContext.ER.AsQueryable();

			if (!string.IsNullOrEmpty(zt.ToString()))
			{
				query = query.Where(s => s.InterviewStatus == zt);
			}
			int totalRecords = query.Count();
			var ER = query.Skip((page - 1) * pageSize).Take(pageSize).ToList();
			return new ERFenYE
			{
				totalRecords = totalRecords,
				er = ER
			};
		}

		public async Task<bool> DeleteERAsync(int id)
		{
			ER er = new ER()
			{
				ResId = id
			};
			myDbContext.ER.Remove(er);
			int changes = await myDbContext.SaveChangesAsync();
			return changes > 0;
		}

		public async Task<ER> GetERByNameAsync(int id)
		{
			return await myDbContext.ER.Where(e => e.ResId == id).FirstOrDefaultAsync();
		}

		public async Task<bool> UpdateERAsync(ER er)
		{
			er.InterviewStatus = 1;
			er.CheckStatus = 2;
			myDbContext.ER.Update(er);
			return await myDbContext.SaveChangesAsync() > 0;
		}
		/// <summary>
		/// 录用申请修改
		/// </summary>
		/// <param name="er"></param>
		/// <returns></returns>
		public async Task<bool> UpdateERApplicationAsync(ER er)
		{
			ER er1 = await myDbContext.ER.Where(e => e.ResId == er.ResId).FirstOrDefaultAsync();
			er1.InterviewStatus = er.InterviewStatus;
			er1.PassCheckComment = er.PassCheckComment;
			myDbContext.ER.Update(er1);
			return await myDbContext.SaveChangesAsync() > 0;
		}

		public async Task<bool> UpdateERSAsync(ER er)
		{
			ER er1 = await myDbContext.ER.Where(e => e.ResId == er.ResId).FirstOrDefaultAsync();
			er1.InterviewStatus = er.InterviewStatus;
			myDbContext.ER.Update(er1);
			return await myDbContext.SaveChangesAsync() > 0;
		}
		/// <summary>
		/// 录用审批修改
		/// </summary>
		/// <param name="er"></param>
		/// <returns></returns>
		/// <exception cref="NotImplementedException"></exception>
		public async Task<bool> UpdateERApprovalAsync(ER er)
		{
			ER er1 = await myDbContext.ER.Where(e => e.ResId == er.ResId).FirstOrDefaultAsync();
			er1.InterviewStatus = er.InterviewStatus;
			er1.PassPassComment = er.PassPassComment;
			myDbContext.ER.Update(er1);
			return await myDbContext.SaveChangesAsync() > 0;
		}
		public ERFenYE DateERAsync(string HumanMajorKindId, string HumanMajorId, string gzc, DateTime startDate, DateTime endDate, int page, int pageSize, int zt)
		{
			var query = myDbContext.ER.AsQueryable();

			if (!string.IsNullOrEmpty(HumanMajorKindId))
			{
				query = query.Where(s => s.HumanMajorKindId == HumanMajorKindId);
			}
			if (!string.IsNullOrEmpty(HumanMajorId))
			{
				query = query.Where(s => s.HumanMajorId == HumanMajorId);
			}
			if (!string.IsNullOrEmpty(gzc))
			{
				query = query.Where(s => s.HumanName.Contains(gzc));
			}

			if (startDate != default && endDate != default)
			{
				query = query.Where(s => s.RegistTime >= startDate && s.RegistTime <= endDate);
			}
			if (!string.IsNullOrEmpty(zt.ToString()))
			{
				query = query.Where(s => s.CheckStatus == zt);
			}
			int totalRecords = query.Count();
			var ER = query.Skip((page - 1) * pageSize).Take(pageSize).ToList();
			return new ERFenYE
			{
				totalRecords = totalRecords,
				er = ER
			};
		}
	}
}
